define(async () => {
    const util = await require("../util");
    const Animation = await require("Animation");

    /**
     * 动画生成器类
     * 
     * 此类还未完善，仍在规划动画系统中
     */
     class Animator {

        static STATE = {
            PLAYING: Symbol("PLAYING"),
            PAUSE: Symbol("PAUSE")
        };

        state = Animator.STATE.PLAYING;
        animations = [];
        loop = false;
        timingFunction;
        parent;
        _currentAnimation;

        constructor() {
            
        }

        update() {
            if(!this.currentAnimation)
                return null;
            this.currentAnimation.update(this.parent);
        }

        interruptAnimation() {
            this.animations = [];
            this._currentAnimation = null;
        }

        addAnimation(animation) {
            util.assert(Animation.isInstance(animation), new TypeError("animation must be an Animation instance"));
            if(!animation.timingFunction && this.timingFunction)
                animation.timingFunction = this.timingFunction;
            this.animations.unshift(animation);
        }

        addAnimations(...animations) {
            animations.forEach(animation => this.addAnimation(animation));
        }

        getAnimation() {
            const animation = this.animations.pop();
            if(this.loop && animation) {
                animation.reset();
                this.addAnimation(animation);
            }
            return animation;
        }

        /**
         * 恢复动画生成
         */
        resume() {
            this.state = Animator.STATE.PLAYING;
        }

        /**
         * 暂停动画生成
         */
        pause() {
            this.state = Animator.STATE.PAUSE;
        }

        get currentAnimation() {
            if(this._currentAnimation && !this._currentAnimation.completed)
                return this._currentAnimation;
            this._currentAnimation = this.getAnimation();
            return this._currentAnimation || null;
        }

        /**
         * 判断是否Animator实例
         * 
         * @param {Any} value 
         * @return {Boolean}
         */
        static isInstance(value) {
            return value instanceof Animator;
        }

    }

    return Animator;
});